<template>
  <div>
    <!-- <div v-if="is_show_tea" class="school_name">{{schoolName}}</div> -->
    <div class="top_change_tea_div">
      <span @click="changeHead" v-show="!is_show_tea" class="change_span click_to_green">
        <img :src="'./static/img/icon-tap.png'" width="15">
        <span>我是教师</span>
      </span>
      <span @click="changeHead" v-show="is_show_tea" class="change_span click_to_green">
        <img :src="'./static/img/icon-tap.png'" width="15">
        <span>我是学生</span>
      </span>
    </div>

    <div class="user_img_div" v-show="!is_show_tea">
      <span class="pic_out_span">
        <imgUpload
          :extClass="'upload_login_img'"
          @uploadOkEvent="uploadOkEvent"
          :height="'80px'"
          :imgSrc="fileSrc"
          :defaultSrc="'./static/img/pic-stu.png'"
          ref="imgUpload"
        ></imgUpload>
      </span>
    </div>

    <div class="am-form-group-out">
      <div class="am-form-group">
        <img :src="'./static/img/icon-person.png'">
        <input
          jump="true"
          type="text"
          autocomplete="off"
          class="am-form-field"
          v-model="username"
          :name="userNameTip"
          @blur="fixScroll"
          v-validate="'required'"
          :placeholder="'请输入'+userNameTip"
        >
      </div>
      <div class="login_line"></div>
      <div class="am-form-group">
        <img :src="'./static/img/icon-lock.png'">
        <input
          jump="true"
          :type="pswType"
          class="am-form-field"
          :name="pswTip"
          v-validate="'required'"
          @blur="fixScroll"
          v-model="mypassword"
          :placeholder="'请输入'+pswTip"
        >
      </div>
    </div>
    <div class="button_div">
      <x-button action-type="submit" class="dskt_btn" type="primary" @click.native="doLogin">登录</x-button>
    </div>
    <!--      <div class="fotget_div" @click="toForget">
        <span>忘记密码</span>
    </div>-->
    <div class="fotget_div" v-show="viewType=='noWx'" @click="doClear">
      <span>清除数据缓存</span>
    </div>
    <div class="fotget_div" v-if="isDebug" @click="toOpenIdLogin">
      <span>OPENID : {{openId}}登录</span>
    </div>
  </div>
</template>

<script>
import userService from "../../shared/service/userService";
import weixinService from "../../shared/service/weixinService";
import baseService from "../../shared/service/baseService";
import imgUpload from "../../components/imgUpload.vue";
import dsktConstant from "../../config/constant";
export default {
  components: {
    imgUpload
  },
  data() {
    var data = {
      head_big: "head_tea.png",
      head_samll: "head_stu.png",
      is_show_tea: false,
      head_big_text: "教师",
      head_samll_text: "学生",
      schoolId: null,
      schoolName: "",
      username: "",
      mypassword: "",
      SYS_NO: dsktConstant.SYS_NO,
      viewType: this.$route.query.viewType,
      openId: this.$route.query.openId,
      schoolNo: null,
      code: null,
      fileSrc: ""
    };
    data.userNameTip = "学号";
    data.pswTip = "身份证号";
    data.pswType = "text";
    data.isDebug = dsktConstant.debug;
    //张斌的ojrgIv8ZFy--9SDEhCYH20ydl2v8 oS-Muv0Apgx6StF5HN0tZqPSh4Vg
    //王桌听得  3170905114  522124199809051242
    //ceshi
    //   if (!data.openId) data.openId = "ceshi";
    if (!data.openId) data.openId = this.$cookie.get(this.$cookie.key.openId);
    //ceshi
    return data;
  },
  mounted() {
    dealKeyOverInput();
  },
  created() {
    if (this.viewType == "noWx") {
      //如果不需要微信自动登录
      this.httpGetSchoolNo();
    } else {
      this.$vux.loading.show();
      var openId = this.$cookie.get(this.$cookie.key.openId);

      if (openId && this.$cookie.get(this.$cookie.key.wxAppId)) {
        this.schoolId = this.$cookie.get(this.$cookie.key.schoolId);
        this.schoolNo = this.$cookie.get(this.$cookie.key.schoolNo);
        this.toOpenIdLogin();
      } else {
        let code = weixinService.getUrlKey("code");
        this.code = code;
        //获取了code 要去回去学校的等信息了
        this.httpGetSchoolNo();
      }
    }
  },
  methods: {
    uploadOkEvent(fileSrc) {
      this.fileSrc = fileSrc;
      this.$refs.imgUpload.setImgSrc(fileSrc);
    },
    fixScroll(){
      AllFixScroll();
    },
    doClear() {
      this.$cookie.clear();
      this.$vux.toast.text("数据缓存清除成功");
    },
    getOpenId(code) {
      weixinService.getInfoByBode(code).then(res => {
        var data = res.data;
        this.openId = data.access_token.openId;
        this.$cookie.set(this.$cookie.key.openId, this.openId);
        //获取了OPENID 在重新调一下
        var path = window.location.href;
        console.log("path", path);
        var s1 = path.indexOf("?code");
        var s2 = path.indexOf("&state=dskt");
        var spath =
          path.substring(0, s1) + path.substring(s2 + "&state=dskt".length);
        window.location.replace(spath);
        //this.toOpenIdLogin();
      });
    },
    httpGetSchoolNo() {
      var schoolNo = this.$route.params.schoolNo;
      if (!schoolNo) {
        schoolNo = this.$cookie.get(this.$cookie.key.schoolNo);
      }
      weixinService.getWxAppId(schoolNo).then(res => {
        var data = res.data;
        var schoolData = data.school;
        var wxData = data.wechatConfig;

        this.schoolId = schoolData.id;
        this.schoolName = schoolData.name;
        this.schoolNo = schoolNo;

        this.$cookie.set(this.$cookie.key.schoolNo, schoolNo);
        this.$cookie.set(this.$cookie.key.schoolId, this.schoolId);
        this.$cookie.set(this.$cookie.key.wxAppId, wxData.appId);
        if (this.viewType != "noWx") {
          //如果不需要微信自动登录
          //然后才能进行调转
          if (this.code) {
            this.getOpenId(this.code);
          } else {
            window.location.replace(weixinService.getAuthUrl());
          }
        }
      });
    },
    changeHead() {
      this.is_show_tea = !this.is_show_tea;
      if (this.is_show_tea) {
        this.head_big_text = "教师";
        this.head_samll_text = "学生";
        this.head_big = "head_tea.png";
        this.head_samll = "head_stu.png";
        this.userNameTip = "用户名";
        this.pswTip = "密码";
        this.pswType = "password";
        if (this.isDebug) {
          this.username = "zhangs";
          this.mypassword = "123456";
            this.openId = "oS-Muv0Apgx6StF5HN0tZqPSh4Vg_zs"; //吴海
        }
      } else {
        if (this.isDebug) {
          /*       this.username = "3170905128";
          this.mypassword = "513701199901161433";
          this.openId = "lyd"; //刘议丹 */
          this.username = "3150703113";
          this.mypassword = "452228199705105034";
          this.openId = "whj"; //吴海见

           this.username = "3181908105";
          this.mypassword = "450422199912243385";
          this.openId = "whc"; //吴海婵

             this.username = "3172312110";
          this.mypassword = "522423199905122311";
          this.openId = "oS-Muv0Apgx6StF5HN0tZqPSh4Vg"; //吴海
/*
                          this.username = "3170905123";
              this.mypassword = "432522199901117539";
              this.openId='oS-Muv0Apgx6StF5HN0tZqPSh4Vg';    */
        }
        //oS-Muv0Apgx6StF5HN0tZqPSh4Vg 线上我的
        this.head_samll_text = "教师";
        this.head_big_text = "学生";
        this.head_samll = "head_tea.png";
        this.head_big = "head_stu.png";
        this.userNameTip = "学号";
        this.pswTip = "身份证号";
        this.pswType = "text";
      }
    },
    toForget() {
      this.$router.push({ name: "forgetPasswordPhone" });
    },
    doLogin() {
      if (this.is_show_tea) {
        if (!this.username) {
          this.$vux.toast.text("请输入用户名");
          return;
        }
        if (!this.mypassword) {
          this.$vux.toast.text("请输入密码");
          return;
        }
        //老师登录
        var reqData = {
          schoolNo: this.schoolNo,
          schoolId: this.schoolId,
          username: this.username,
          password: this.mypassword,
          openId: this.openId,
          sysNo: this.SYS_NO
        };
        if (this.viewType == "noWx") {
          userService.login(reqData).then(res => {
            this.loginOk(res.data);
          }).catch((res) => {
            this.$vux.toast.text(res.data.message);
          });;
        } else {
          userService.staffBind(reqData).then(res => {
            this.toOpenIdLogin();
          }).catch((res) => {
            this.$vux.toast.text(res.data.message);
          });
        }
      } else {
        if (!this.username) {
          this.$vux.toast.text("请输入学号");
          return;
        }
        if (!this.mypassword) {
          this.$vux.toast.text("请输入身份证号");
          return;
        }
        //学生登录
        var reqData = {
          schoolNo: this.schoolNo,
          no: this.username,
          idNo: this.mypassword,
          schoolId: this.schoolId,
          openId: this.openId,
          sysNo: this.SYS_NO,
          headImg: this.fileSrc
        };
        userService.stuBind(reqData).then(res => {
          this.toOpenIdLogin();
        }).catch((res) => {
            this.$vux.toast.text(res.data.message);
        });;
      }
    },
    toOpenIdLogin() {
      userService.openIdLogin(this.openId, this.SYS_NO).then(res => {
        this.$vux.loading.hide();
        var data = res.data;
        if (!data) {
          //  alert("没有绑定过");
        } else {
          if (data.studentFile) {
            this.is_show_tea = false;
          } else if (data.staffFile) {
            this.is_show_tea = true;
          }
          this.loginOk(data);
        }
      });
    },
    loginOk(data) {
      var user = data.user;
      if (!data.token) {
        data.token = "zbzbzbzb";
        user = data;
      }
      this.$cookie.set(this.$cookie.key.token, data.token);
      this.$cookie.set(this.$cookie.key.userId, user.id);
      this.$cookie.delete(this.$cookie.key.NOWTYPE);
      if (user.staffFile) {
        var staffFile = user.staffFile;
        this.$cookie.setLoginUserData(staffFile);
          this.$cookie.delete(this.$cookie.key.openOrderStuFileId);
          this.$cookie.delete(this.$cookie.key.openOrderStuUserId);
          this.$cookie.delete(this.$cookie.key.openOrderStuName);
          this.$cookie.delete(this.$cookie.key.openOrderStuCellphone);

      } else if (user.studentFile) {
        var studentFile = user.studentFile;
        this.$cookie.setLoginUserData(studentFile,studentFile.id);
        this.$cookie.set(this.$cookie.key.openOrderStuFileId,studentFile.id);
        this.$cookie.set(this.$cookie.key.openOrderStuUserId,user.id);
        this.$cookie.set(this.$cookie.key.openOrderStuName,studentFile.name);
        this.$cookie.set(this.$cookie.key.openOrderStuCellphone,studentFile.cellphone);

      }
      //data.user.category 1学生 2非学生
      if (this.is_show_tea && user.category == 2) {
        this.$cookie.set( this.$cookie.key.LoginUserType,dsktConstant.TEACHER);
        this.$vux.toast.text("登录成功");
        this.$router.replace({ name: "tea_home" });
      } else if (!this.is_show_tea && user.category == 1) {
        this.$cookie.set( this.$cookie.key.LoginUserType,dsktConstant.STUDENT);
        this.$vux.toast.text("登录成功");
        this.$router.replace({ name: "stu_score" });
      } else {
        this.$vux.toast.text("角色错误");
      }
    }
  }
};
</script>

<style scoped>
@import "../../assets/scss/login.scss";
</style>
